package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdczbhslBaDetailBo;
import com.ruoyi.report_tg.domain.vo.TdczbhslBaDetailVo;
import com.ruoyi.report_tg.domain.TdczbhslBaDetail;
import com.ruoyi.report_tg.mapper.TdczbhslBaDetailMapper;
import com.ruoyi.report_tg.service.ITdczbhslBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的承载比（CBR）含水率试验记录详细Service业务层处理
 *
 * @author zyb
 * @date 2024-10-29
 */
@RequiredArgsConstructor
@Service
public class TdczbhslBaDetailServiceImpl implements ITdczbhslBaDetailService {

    private final TdczbhslBaDetailMapper baseMapper;

    /**
     * 查询土的承载比（CBR）含水率试验记录详细
     */
    @Override
    public TdczbhslBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询土的承载比（CBR）含水率试验记录详细列表
     */
    @Override
    public TableDataInfo<TdczbhslBaDetailVo> queryPageList(TdczbhslBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdczbhslBaDetail> lqw = buildQueryWrapper(bo);
        Page<TdczbhslBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的承载比（CBR）含水率试验记录详细列表
     */
    @Override
    public List<TdczbhslBaDetailVo> queryList(TdczbhslBaDetailBo bo) {
        LambdaQueryWrapper<TdczbhslBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdczbhslBaDetail> buildQueryWrapper(TdczbhslBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdczbhslBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, TdczbhslBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBaRowNumber() != null, TdczbhslBaDetail::getBaRowNumber, bo.getBaRowNumber());
        lqw.eq(bo.getHansl() != null, TdczbhslBaDetail::getHansl, bo.getHansl());
        lqw.eq(bo.getGanmd() != null, TdczbhslBaDetail::getGanmd, bo.getGanmd());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh()), TdczbhslBaDetail::getHeh, bo.getHeh());
        lqw.eq(bo.getHezl() != null, TdczbhslBaDetail::getHezl, bo.getHezl());
        lqw.eq(bo.getHejstzl() != null, TdczbhslBaDetail::getHejstzl, bo.getHejstzl());
        lqw.eq(bo.getHejgtzl() != null, TdczbhslBaDetail::getHejgtzl, bo.getHejgtzl());
        lqw.eq(bo.getShifzl() != null, TdczbhslBaDetail::getShifzl, bo.getShifzl());
        lqw.eq(bo.getGantzl() != null, TdczbhslBaDetail::getGantzl, bo.getGantzl());
        lqw.eq(bo.getHehId() != null, TdczbhslBaDetail::getHehId, bo.getHehId());
        return lqw;
    }

    /**
     * 新增土的承载比（CBR）含水率试验记录详细
     */
    @Override
    public Boolean insertByBo(TdczbhslBaDetailBo bo) {
        TdczbhslBaDetail add = BeanUtil.toBean(bo, TdczbhslBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改土的承载比（CBR）含水率试验记录详细
     */
    @Override
    public Boolean updateByBo(TdczbhslBaDetailBo bo) {
        TdczbhslBaDetail update = BeanUtil.toBean(bo, TdczbhslBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdczbhslBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的承载比（CBR）含水率试验记录详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
